import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getLocal } from '@/utils'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: { isAuth: false, title: '登录' },
      component: () => import('@/views/login.vue'),
    },
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/layout/index.vue'),
      redirect: 'home',
      meta: { isAuth: true },
      children: [
        {

          path: '/home',
          name: 'home',
          component: () => import('@/views/home/index.vue'),
          // meta: { isAuth: true, title: '首页' },

        },
        {
          path: '/personal',
          name: 'personal',
          component: () => import('@/views/personal/index.vue'),
          // meta: { isAuth: true, title: '个人主页' },
          children: [
            {
              path: 'article_favor',
              name: 'article_favor',
              component: () => import('@/views/personal/favor/article_favor.vue')
            },
            {
              path: 'article_comment',
              name: 'article_comment',
              component: () => import('@/views/personal/favor/article_comment.vue')
            },
          ]
        },

        {
          path: '/article',
          name: 'Article',
          component: () => import('@/views/article/index.vue'),
          redirect: { name: 'create_article' },
          children: [
            {
              'path': "create_article",
              'name': 'create_article',
              component: () => import('@/views/article/article_edit/create_article.vue')
            },
            {
              'path': "update_article",
              'name': 'update_article',
              component: () => import('@/views/article/article_edit/update/index.vue'),
            },
            {
              path: 'update_article/:id',
              name: 'update_article_detail',
              component: () => import('@/views/article/article_edit/update/update_detail.vue')
            },
            {
              path: 'article_status',
              name: 'article_status',
              component: () => import('@/views/article/article_status/index.vue'),
              redirect: { name: 'published_article' },
              children: [
                {
                  path: 'passed_article',
                  name: 'passed_article',
                  component: () => import('@/views/article/article_status/passed_article.vue')
                }, {
                  path: 'unpassed_article',
                  name: 'unpassed_article',
                  component: () => import('@/views/article/article_status/unpassed_article.vue')
                },
                {
                  path: 'awaitpass_article',
                  name: 'awaitpass_article',
                  component: () => import('@/views/article/article_status/awaitpass_article.vue')
                },
                {
                  path: 'published_article',
                  name: 'published_article',
                  component: () => import('@/views/article/article_status/published_article.vue')
                },
                {
                  path: 'draft_article',
                  name: 'draft_article',
                  component: () => import('@/views/article/article_status/draft_article.vue')
                }, {
                  path: 'deleted_article',
                  name: 'deleted_article',
                  component: () => import('@/views/article/article_status/deleted_article.vue')
                }
              ]
            },
            {
              path: 'article_collection',
              name: 'article_collection',
              component: () => import('@/views/article/article_collected/index.vue')
            },
            {
              path: 'article_history',
              name: 'article_history',
              component: () => import('@/views/article/article_history/index.vue')
            },
          ]
        },
        {
          path: 'account_verified',
          name: 'account_verified',
          component: () => import('@/views/verified/account_verified.vue')
        },
        {
          path: 'article_verified',
          name: 'article_verified',
          component: () => import('@/views/verified/article_verified.vue')
        },
        {
          path: 'conversation_list',
          name: 'conversation_list',
          component: () => import('@/views/conversation/conversationList.vue')
        },
        {
          path: 'article_detail/:id',
          name: 'article_detail',
          component: () => import('@/views/article/article_detail/index.vue')
        }
      ]
    },
    { 
      path: '/:pathMatch(.*)',
      redirect: { name: 'home' }
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth && !getLocal('USER_INFO')) {
    next('/login')
    ElMessage({
      type: 'warning',
      message: '请先登录!'
    })
  } else {
    next()
  }
})

export default router
